package top.saybook.crawlerutil;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import top.saybook.entity.WebUrl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CrawlerUtil {
    private static PoolingHttpClientConnectionManager cm ;
    private static CloseableHttpClient httpClient;
    static{
        cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(300);
        httpClient = HttpClients.custom().setConnectionManager(cm).build();
    }

    // 爬取所有书籍
    public static List<Map> crawlerBook(String webUrl,String ele){
        List<Map> crawlerBookList = new ArrayList<>();

        HttpGet httpGet = new HttpGet(webUrl);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode()==200){
                String content = EntityUtils.toString(response.getEntity(), "utf-8");
                Document document = Jsoup.parse(content);
                Elements bookList = document.select(ele);
                for (Element book : bookList) {
                    Map map  = new LinkedHashMap();
                    map.put("bookName",book.text());
                    map.put("linkUrl",book.attr("href"));
                    crawlerBookList.add(map);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return crawlerBookList;
    }

    public static List<Map> crawlerBookChater(String Bookurl,String ele){
        List<Map> crawlerChapterList = new ArrayList<>();

        HttpGet httpGet = new HttpGet(Bookurl);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode()==200){
                String content = EntityUtils.toString(response.getEntity(), "utf-8");
                Document document = Jsoup.parse(content);
                Elements chapterList = document.select(ele);
                for (Element book : chapterList) {
                    Map map  = new LinkedHashMap();
                    map.put("chapterName",book.text());
                    map.put("linkUrl","http://www.xbiquge.la"+book.attr("href"));
                    crawlerChapterList.add(map);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return crawlerChapterList;
    }

    public static String crawlerChapterContent(String chapterUrl,String ele){

        HttpGet httpGet = new HttpGet(chapterUrl);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode()==200){
                String content = EntityUtils.toString(response.getEntity(), "utf-8");
                Document document = Jsoup.parse(content);
                Element element = document.select(ele).first();
                String contentHtml = element.html();
                contentHtml = contentHtml.replace("<p><a href=\"http://koubei.baidu.com/s/xbiquge.la\" target=\"_blank\">亲,点击进去,给个好评呗,分数越高更新越快,据说给新笔趣阁打满分的最后都找到了漂亮的老婆哦!</a><br>手机站全新改版升级地址：http://m.xbiquge.la，数据和书签与电脑站同步，无广告清新阅读！</p>", "sayBook 无广告清新阅读！");
                return contentHtml;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }
}
